home *** CD-ROM | disk | FTP | other *** search
/ Fritz: All Fritz / All Fritz.zip / All Fritz / FILES / PROGMISC / FPCDOCS.LZH / MEMORY.DOC < prev    next >
Text File  |  1988-06-01  |  11KB  |  265 lines

  1. VI.   EXTENDED MEMORY 
  2.  
  3.  
  4.  
  5. 1.   MEMORY MAP OF F-PC
  6.  
  7.  
  8.  
  9. F-PC was built to handle a class of programming problems that
  10. require very large memory and cannot be handled well on any normal 
  11. 64K implementation of Forth because of space constraints. 
  12.  
  13. The memory map of F-PC can be shown schematically as follows:
  14.  
  15.  
  16.                 |---------------------| ?CS:
  17.          ^      |                     |
  18.          |      |       Forth         |
  19.          |      |       Kernel        |
  20.          |      |                     |
  21.          |      |---------------------|
  22.          |      |       System        |
  23.          |      |     Extensions      |
  24.          |      |---------------------| HERE, DP @
  25.          |      |     Dictionary      | PAD, DP + 80
  26.          |      |     Free space      |
  27.         64k     |         |           |
  28.        bytes    |         v           |
  29.          |      |         ^           |
  30.          |      |         |           |
  31.          |      |     Data stack      |
  32.          |      |---------------------| SP0 @
  33.          |      |        TIB          | TIB, 'TIB @
  34.          |      |         |           |
  35.          |      |         v           |
  36.          |      |         ^           |
  37.          |      |         |           |
  38.          v      |    Return stack     |
  39.                 |---------------------| RP0  @, End code space.
  40.  
  41.                 |---------------------| YSEG @, Start head space.
  42.          ^      |       hash table    |
  43.          |      |---------------------|
  44.          |      |                     |
  45.         64k     |       headers       |
  46.        bytes    |                     |
  47.          |      |---------------------| YHERE,  YDP @
  48.          |      |       header        |
  49.          v      |     free space      | YHERE + 65535
  50.                 |---------------------|
  51.  
  52.                 |---------------------| TSEGB, TSEGE
  53.          ^      |                     |
  54.      64k bytes  |     Edit Buffer     |
  55.          |      |                     |
  56.          |      |---------------------| LSEG
  57.          |      |  Line pointer list  |
  58.          |      |---------------------| DSEG
  59.          |      |  Lines deleted buf  |
  60.          |      |---------------------| RSEG
  61.          v      | Restore display buf |
  62.                 |---------------------|
  63.  
  64.                 |---------------------| XSEG @, Start list space.
  65.          ^      |                     |
  66.          |      |       List of       |
  67.          |      |                     |
  68.         64k     |  Colon Definitions  |
  69.        bytes    |                     |
  70.         or      |     and Strings     |
  71.        more     |                     |
  72.          |      |---------------------| XHERE,  XDP @
  73.          |      |                     |
  74.          |      |       header        |
  75.          |      |     free space      | 
  76.          v      |                     |
  77.                 |---------------------|
  78.  
  79.  
  80.  
  81.  
  82. F-PC allows the list (colon space) segment to be much larger than
  83. 64K bytes.  Strings have also been moved out of code space back 
  84. into LIST space, which means there is effectively unlimited space 
  85. for text messages.  In fact the only real limitation in size comes 
  86. from the fact that there are only 64k available for heads.  
  87. Calculations indicate there is room for about 3300 definitions on 
  88. top of the current system of about 2000 words.  This calculation 
  89. is based on an average name length of 5 characters, with an 
  90. average header length of 12 bytes.  With 5300 total definitions, 
  91. less than 30k of code space would be used for code fields of colon 
  92. definitions, leaving the remaining 30+k of code space for 
  93. variables and arrays.  Not an infinitely large application 
  94. obviously but probably large enough for most (99%) applications. 
  95.  
  96. F-PC places a segment (ES) and and offset (IP) on the return stack
  97. for each nest operation. Only the starting relative segment is 
  98. compiled into the code field, and the conversion to absolute 
  99. segment is performed by NEST.  This makes the code fully 
  100. relocatable as is required by the DOS environment.  Some 
  101. performance could be gained by using absolute segment addressing, 
  102. and performing a conversion at save and load time to and from 
  103. relative and absolute, but I have not done this, and don't plan to 
  104. for now.  The performance gain would come from having a simpler 
  105. NEST, what would not need to perform the relative to absolute 
  106. conversion at runtime.  This system looses about 20% in 
  107. nest/unnest performance due exclusively to that conversion. 
  108.  
  109. CS and DS always point to the code space, and so all assembly 
  110. language operations work with data in the code segment unless a 
  111. special operator like @L or !L is used to reach an external memory 
  112. area.  This means there is no penalty to be paid for most Forth 
  113. operations. 
  114.  
  115. Here is the data structure used for a colon definition in F-PC:
  116.  
  117.  
  118.  
  119.                                      ----------------
  120.             HEAD SPACE               [ VIEW offset  ] VIEW
  121.                                      [ LINK pointer ] LINK
  122.                                      [      83 hex  ] NAME
  123.                                      [   H          ]
  124.                                      [   E          ]
  125.                                      [   X + 80 hex ]
  126.                 Points to CODE space [ CFA pointer  ] --------v
  127.                                      ----------------         |
  128.                                                               |
  129.                                      ----------------         |
  130.             CODE SPACE               [     CALL     ] CFA <---<
  131.                                      [     NEST     ]
  132.                 Points to LIST space [ LIST POINTER ] BODY >--v
  133.                                      ----------------         |
  134.                                                               |
  135.                                      ----------------         |
  136.             LIST SPACE               [ cfa of (LIT) ] LIST <--<
  137.                                      [     16       ]
  138.                                      [ cfa of BASE  ]
  139.                                      [ cfa of   !   ]
  140.                                      [ cfa of UNNEST]
  141.                                      ----------------
  142.  
  143.  
  144. 2.   EXTENDED MEMORY WORD SET
  145.  
  146.  
  147.  
  148. A complete set of words are defined in F-PC to let the user
  149. accessing the extended memory very conveniently.  The standard 
  150. Forth memory accessing words are retained for addressing the 
  151. code/data segment in the memory.  Generic extended memory words 
  152. are appended with the letter L, and they require address 
  153. specifications in segment/offset pairs.  Words addressing the list 
  154. space have X prefix and words addressing the head space have Y 
  155. prefix. 
  156.  
  157. Generic Extended Memory Words are those which require explicit 
  158. segment information with the 16 bit address offset.  They are the 
  159. basic word set from which other segment specific memory words are 
  160. derived.  This word set includes the following words: 
  161.  
  162. @L        ( seg addr -- n )        Fetch 16 bit integer.
  163.  
  164. !L        ( n seg addr -- )        Store 16 bit integer.
  165.  
  166. C@L       ( seg addr -- b )        Fetch a byte.
  167.  
  168. C!L       ( b seg addr -- )        Store a byte.
  169.  
  170. CMOVEL    ( seg addr seg' addr' n -- )
  171.                                    Move n bytes from seg-addr to
  172.                                    seg'-addr'.
  173.  
  174. CMOVEL>   ( seg addr seg' addr' n -- )
  175.                                    Move n bytes in revered order.
  176.  
  177. LFILL     ( seg addr n b -- )      Fill n bytes at seg-addr 
  178.                                    with b.
  179.  
  180. LDUMP     ( addr n -- )            Dump n byte from segment pointed
  181.                                    to by DUMPSEG with addr offset.
  182.  
  183.  
  184. Following are words addressing the list space: 
  185.  
  186.  
  187. X@        ( addr -- n )            Fetch an integer in list space 
  188.  
  189. X!        ( n addr -- )            Store integer to list space 
  190.  
  191. XC@       ( addr -- b )            Fetch byte.
  192.  
  193. XC!       ( b addr -- )            Store byte.
  194.  
  195. X,        ( n -- )                 Compile integer to top of
  196.                                    the list dictionary. 
  197.  
  198. XC,       ( b -- )                 Compile byte to the list 
  199.                                    dictionary.
  200.  
  201. XDUMP     ( addr n -- )            Dump n bytes in the list 
  202.                                    segment.
  203.  
  204. ?XS:      ( -- seg )               Return the base of the list 
  205.                                    segment.
  206.  
  207. XDP       ( -- addr )              Pointer to top of list 
  208.                                    dictionary. 
  209.  
  210. XHERE     ( -- addr )              Address of top of list 
  211.                                    dictionary. 
  212.  
  213. XPERFORM  ( addr -- )              Execute word at addr.
  214.  
  215.  
  216.  
  217. The corresponding words addressing into the head space are: 
  218.  
  219.  
  220. Y@        ( addr -- n )            Fetch integer from head space. 
  221.  
  222. Y!        ( n addr -- )            Store integer to head space. 
  223.  
  224. YC@       ( addr -- b )            Fetch byte.
  225.  
  226. YC!       ( b addr -- )            Store byte.
  227.  
  228. Y,        ( n -- )                 Compile integer to head 
  229.                                    dictionary.
  230.  
  231. YCSET     ( b addr -- )            Set bits in a byte.
  232.  
  233. YDUMP     ( addr n -- )            Dump n bytes from addr in the
  234.                                    head space. 
  235.  
  236. YDP       ( -- addr )              Pointer to top of head 
  237.                                    dictionary.
  238.  
  239. YHERE     ( -- addr )              Address of top of head 
  240.                                    dictionary.
  241.  
  242.  
  243.  
  244. 3.   MEMORY ALLOCATION
  245.  
  246.  
  247.  
  248. Finally, two very important words which allows you to allocate and 
  249. deallocate extended memory by asking the DOS for unused free 
  250. memory:
  251.  
  252.  
  253. ALLOC     ( n1 -- n2 seg flag )    Request n bytes of free memory
  254.                                    from DOS.  Flag=0 if ok, 8 if
  255.                                    not enough memory.  Seg is the
  256.                                    segment base of the allocated
  257.                                    memory.  n2 byte actually
  258.                                    allocated.
  259.  
  260. DEALLOC   ( seg -- flag )          Release a block of memory to 
  261.                                    DOS.  Seg should be the same
  262.                                    as obtained from ALLOC.
  263.                                    Flag=0 if ok, 9 if seg is not
  264.                                    valid. 
  265.